﻿using PerpheralDevice.IRepository;
using PerpheralDevice.Units;
using System.Serialization;

namespace AppMachine.Domain.LogicCom.AppComs;

/// <summary>
/// 应用组件(输出开关组件)
/// </summary>
public class ComSwitchRW : ComSwitch {

    public ComSwitchRWParam ComSwitchRWParam { get; set; }
    public override bool IsDisabled => this.ComSwitchRWParam.IsDisbaled;
    public override bool IsReversed => this.ComSwitchRWParam.IsReversed;
    public int? DelayMiliSecs => this.ComSwitchRWParam.DelayMiliSecs;
    public ComSwitchRW(IPerpDevRepository perpDevRepository, IComConfig comConfig, IComRepository comRepository)
        : base(perpDevRepository, comConfig, comRepository) {

        this.ComSwitchRWParam = YamlExtend.GetObject<ComSwitchRWParam>(this.ComConfig.ComParamSerialized) ?? new();
    }

    public override bool? IsOpen => this.PerpDev.Find<SwitchRW>(this.ComConfig.PerpSeatID!)?.View().IsOpen;

    public ComSwitchRW Open() {
        this.PerpDev.Find<SwitchRW>(this.ComConfig.PerpSeatID!).Open();
        return this;
    }

    public ComSwitchRW Shut() {
        this.PerpDev.Find<SwitchRW>(this.ComConfig.PerpSeatID!).Shut();
        return this;
    }

    public ComSwitchRW View() {
        this.PerpDev.Find<SwitchRW>(this.ComConfig.PerpSeatID!).View();
        return this;
    }

    public ComSwitchRW IsDisabledSet(bool isDisabled) {
        this.ComSwitchRWParam.IsDisbaled = isDisabled;
        return this;
    }

    public ComSwitchRW IsReversedSet(bool isReversed) {
        this.ComSwitchRWParam.IsReversed = isReversed;
        return this;
    }

    public ComSwitchRW DelayMiliSecsSet(int? miliSecs) {
        this.ComSwitchRWParam.DelayMiliSecs = miliSecs;
        return this;
    }
    public override AppCom Save() {
        (this.ComConfig as AppComConfig)!.ComParamSerialized = YamlExtend.GetString(this.ComSwitchRWParam);
        return base.Save();
    }
}
